-
Notifications
You must be signed in to change notification settings - Fork 27.4k
fix(ngRequired): set valid to false if select value is required (value is not in options) #13354
Conversation
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed, please reply here (e.g.
|
b5f4a2e
to
e27f25a
Compare
e27f25a
to
391abe6
Compare
I signed it! |
391abe6
to
7590d7a
Compare
Thanks for the PR. It's going in the right direction, but there are a few things that should be done differently.
Do you want to take another shot at this? |
|
||
// In case of multiple view model is an array so validate all view values | ||
// if one of them doesn't match set isValidOption to false | ||
if (multiple) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we want this behavior with multiple
the required error should only be set if no option is selected. If it's multiple and a at least option is selected, then the required error doesn't make much sense. Maybe we should actually introduce a new kind of error called optionMismatch
or something like that.
@Narretz
and it is already overwritten in selectDirective with same code in case of multiple
why this duplication ? i think that the overwritten of the $isEmpty function in ngOptionsDirective is |
CLAs look good, thanks! |
…e is not in options)
…ix_ng-require_invalid_select
7590d7a
to
20e2245
Compare
I have improved by solution 2 - I have override $isEmpty function to check if option is valid or not
3 - if select is multiple and value is an array we check that at least one of the values of the array must be in values of the select options to be valid 4 - I have removed since it is already overwritten in selectDirective with same code in case of multiple
@Narretz Please check it tell me your feedback and i just have a question about the data in the model I have a problem with this test cases
so i change it to be
Thanks |
e1bbff4
to
b1946fd
Compare
@@ -2532,8 +2532,8 @@ describe('ngOptions', function() { | |||
expect(scope.value).toBe(false); | |||
|
|||
scope.$apply('required = true'); | |||
expect(element).toBeValid(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Imo, this is not right. required means that at least one option must be selected, it doesn't matter if the value is false. I'll look into why this test is failing.
Thanks for updating this! The test fails because you are not setting isOptionValid to true whenever an option is selected from the ui, i.e. in the readValue fn. I'll take this PR from here, thanks for your work! |
a6e7b8d
to
028dc11
Compare
…her impllementation )
cf214f7
to
e48a6b0
Compare
…isSelectedOptionValid function)
@Narretz Thank for replying
I check if the value found in selectValueMap to know if option is valid or not. |
Hi @Narretz, you mentioned you were taking the PR from here, did you ever commit this? This seems to still be an issue for me and I'd love to see what the fix was for this. Thanks in advance! |
@changus You are right. This is still on my radar. I had some other ideas about the implementation, that's why it was never merged. I'll try to get it in until the end of the month. |
…wn option selected With this patch, a regular / ngOptions select with the "required" attribute will now add the "required" error to the ngModel if the *unknown" option is selected. Previously, the error was only set if the *empty* option was selected. The unknown option is selected if the model is set to a value that does not match any option. If the select has an empty option, this option is selected when the model is `null` or `undefined`. For both, "required" means that the select is invalid, as no correct option selection is provided. Closes angular#13354 Fixes angular#13172
…wn option selected With this patch, a regular / ngOptions select with the "required" attribute will now add the "required" error to the ngModel if the *unknown" option is selected. Previously, the error was only set if the *empty* option was selected. The unknown option is selected if the model is set to a value that does not match any option. If the select has an empty option, this option is selected when the model is `null` or `undefined`. For both, "required" means that the select is invalid, as no correct option selection is provided. Closes angular#13354 Fixes angular#13172
We've decided that this should be handled by the application instead of imposing it by default. To make this easier, 1.6.5 will expose info about the unknown option via the selectCtrl: 6fc0f02 |
This pull request solve Issue 13172
#13172
i validate that the view value should be one of the select option
so i overrideng ModelCtrl.$validators.required function in ngOptionsDirective
we have the following cases
1 - case attr.required is false i return true
2 - case $isEmpty(viewValue) return false
3 - case is required and not empty now we need to validate that value is one of the options
and in this case we have two subcases
a - select in multiple and value is an array
we check that all the values of the array must be in of the select options to return true
b - select is a single value so this value must be one of the select options to return true
i think this will solve the problem,